package pt.inescid.components.policyengine.heimdall.shared;

import java.util.Vector;

public class SharedMemory {

	public static final int QUEUE_LOCAL_INPUT = 8;
	public static final int QUEUE_RAW_EVENTS = 0;
	public static final int QUEUE_AUTHZ_RESPONSES = 1;
	public static final int QUEUE_RAW_EVALUATIONS = 2;
	public static final int QUEUE_EVALUATION_RESPONSES = 3;
	public static final int QUEUE_USAGE_NOTIF = 4;
	public static final int QUEUE_INFORM_REQ = 5;
	public static final int QUEUE_INFORM_RES = 6;
	public static final int QUEUE_NODE_RES = 7;
		
	// shared memory into Local Interface
	private static Vector localInput = new Vector();

	// shared memory for Local Interface / Policy Monitor
	private static Vector rawEvents = new Vector();
	private static Vector authorizationResponses = new Vector();
	
	// shared memory for Policy Monitor / PDP
	private static Vector rawEvaluations = new Vector();
	private static Vector evaluationResponses = new Vector();
	
	// shared memory for Policy Monitor / PIP
	private static Vector usageNotifications = new Vector();

	// shared memory for PDP / PIP
	private static Vector informationRequests = new Vector();
	private static Vector informationResponses = new Vector();

	// shared memory for PIP / nodes
	private static Vector nodeResponses = new Vector();

	/* ===========================================================
	==============================================================
	=============                    =============================
	=============      Add       =============================
	=============                    =============================
	==============================================================
	=========================================================== */
	public static void addToQueue(Object obj, int queue) {
	
		Vector queueObj = null;
		
		switch (queue) {
		case QUEUE_LOCAL_INPUT:
			queueObj = localInput;
			break;
		case QUEUE_RAW_EVENTS:
			queueObj = rawEvents;
			break;
		case QUEUE_AUTHZ_RESPONSES:
			queueObj = authorizationResponses;
			break;
		case QUEUE_RAW_EVALUATIONS:
			queueObj = rawEvaluations;
			break;
		case QUEUE_EVALUATION_RESPONSES:
			queueObj = evaluationResponses;
			break;
		case QUEUE_USAGE_NOTIF:
			queueObj = usageNotifications;
			break;
		case QUEUE_INFORM_REQ:
			queueObj = informationRequests;
			break;
		case QUEUE_INFORM_RES:
			queueObj = informationResponses;
			break;
		default:
			return;
		}
		
		synchronized (queueObj) {
			queueObj.add(obj);
		}
	} // addToQueue
	
	
	/* ===========================================================
	==============================================================
	=============                    =============================
	=============      Get       =============================
	=============                    =============================
	==============================================================
	=========================================================== */
	public static Object getNextFromQueue(int queue) {

		Vector queueObj = null;
		Object objToReturn = null;
		
		switch (queue) {
		case QUEUE_RAW_EVENTS:
			queueObj = rawEvents;
			break;
		case QUEUE_AUTHZ_RESPONSES:
			queueObj = authorizationResponses;
			break;
		case QUEUE_RAW_EVALUATIONS:
			queueObj = rawEvaluations;
			break;
		case QUEUE_EVALUATION_RESPONSES:
			queueObj = evaluationResponses;
			break;
		case QUEUE_USAGE_NOTIF:
			queueObj = usageNotifications;
			break;
		case QUEUE_INFORM_REQ:
			queueObj = informationRequests;
			break;
		case QUEUE_INFORM_RES:
			queueObj = informationResponses;
			break;
		default:
			return (null);
		}
		
		synchronized (queueObj) {
			if (queueObj.size() > 0) {
				objToReturn = queueObj.elementAt(0);
				queueObj.removeElementAt(0);
			}
		}
		
		return (objToReturn);
	} // getNextFromQueue
	
	
}; // SharedMemory
